package com.ffzx.commerce.framework.utils;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Component;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 权限标签
 * @author zhugj
 * @date 2016年2月26日 下午2:35:06
 * @version 1.0 
 */
@Component("permissionDirective")
public class PermissionDirective implements TemplateDirectiveModel{
	public void execute(Environment env, @SuppressWarnings("rawtypes") Map params, TemplateModel[] model, TemplateDirectiveBody body) throws TemplateException, IOException {  
        
        //从Redis中获取权限
        Set<String> permissionSet = RedisWebUtils.getPermission();
        Object permissionObj = params.get("name");  
        String permission = (permissionObj == null ? "" : permissionObj.toString());  
        if(null != permissionSet && permissionSet.contains(permission)){  
            if(body != null){  
                body.render(env.getOut());  
            }  
        }else{  
            env.getOut().write("");  
        }  
    }  
}  
